import treetaggerwrapper
from context_functions import *
import sys
from numpy import array


import psycopg2

if len(sys.argv)<2: 
	print "\n*** usage:",sys.argv[0]," [word file path]"
	exit()

wordfilepath = sys.argv[1]

treetagger_dir = '/opt/TreeTagger'
language="en"
tagger = treetaggerwrapper.TreeTagger(TAGLANG=language,TAGDIR=treetagger_dir)


myconnection = "dbname='sa' user='ecos' host='localhost' password='ecos'"

conn = psycopg2.connect(myconnection)
cur = conn.cursor()

sql = """select identifier,title,abstract from publication where abstract <> '$$$'"""

table = []

cur.execute(sql)
table.append(cur.fetchall())

documents = {}
docids = []
f = open("log.txt","w")
for rows in table:
    for r in rows:
	tags = tagger.TagText(r[1]+" "+r[2])
	tagsl = []
	for t in tags:
		tst = t.split("\t")
		if len(tst)>2 and (tst[1] in ["NN", "NNS", "NP", "NPS", "FW"]):
			if tst[2] == "<unknown>":
				ts = tst[0]
			else:
				ts=tst[2]
			tagsl.append(ts)
	documents[r[0]] = " " + str(tagsl).replace("['","").replace("', '"," ").replace("']","").lower() + " "
	docids.append(r[0])
	#print documents[r[0]]
	f.write(r[0] + ":\t" + documents[r[0]]+"\n\n")
f.close()
cur.close()
conn.close()


wordfile = open(wordfilepath,"r")
wordlist = []
for line in wordfile:
	if line != "\n":
		w = line.strip().replace("\n","")
		wordlist.append(w)

matrix = []
mapping = {}

for w in wordlist:
	n = []
	for idd in docids:
		d = documents[idd]
		wp = " " + w + " "
		#print w,"-->",d
		v = len(d.split(wp)) - 1
		#if v > 0:
		#	print v,wp,"-->",d
		n.append(v)
	matrix.append(n)
matrix = array(matrix)
	
writeContext("con.rcf",matrix,wordlist,docids)
writeContext("con-bin.rcf",matrix,wordlist,docids,bin=True)
writeContext("con-bin-inv.rcf",matrix.T,docids,wordlist,bin=True)

'''
for w in wordlist:
	if sum(matrix[mapping[w]]) > 0: 
		print w,sum(matrix[mapping[w]])


salida = "[Relational Context]\nDefault Name\n[Binary Relation]\nName_of_dataset\n"

for w in wordlist:
	if sum(matrix[mapping[w]]) > 0:
		salida += str(w) + " | "
salida += "\n"
for c in documents.keys():
    salida += str(c) + " | "
salida += "\n"

for w in wordlist:
	if sum(matrix[mapping[w]]) > 0: 
		for v in matrix[mapping[w]]:
			if int(v) < 1:
				salida += str(v) + " "
			else:
				salida += "1 "
		salida += "\n"

salida += "[END Relational Context]"
f = open("contexto.rcf","w")
f.write(salida);
f.close()
'''
